package com.zmn.oms.dubbo.impl.normal.master;

import com.alibaba.fastjson.JSON;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.work.OrderWorkPositionBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderPayConsts;
import com.zmn.oms.common.dio.normal.acceptance.AddAcceptanceDIO;
import com.zmn.oms.common.dro.normal.acceptance.AddOrderAcceptanceDRO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.interfaces.normal.master.MasterOrderAcceptanceRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.acceptance.OrderWorkAcceptanceDTO;
import com.zmn.oms.model.dto.acceptance.ResultOrderAcceptanceDTO;
import com.zmn.oms.model.dto.work.masterwork.CompleteDTO;
import com.zmn.oms.services.interfaces.pay.OrderPayService;
import com.zmn.oms.zmn.business.interfaces.acceptance.OrderWorkAcceptanceBService;
import com.zmn.oms.zmn.normal.business.interfaces.work.masterwork.ZsNormalMasterWorkBService;
import com.zmn.pay.common.constant.PayConsts;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * 类描述：
 *
 * @author wangxiaokun
 * @since 2021/09/24 15:59
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 10000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class MasterOrderAcceptanceRemoteServiceImpl implements MasterOrderAcceptanceRemoteService {

    @Autowired
    private OrderWorkAcceptanceBService orderWorkAcceptanceBService;
    @Autowired
    protected ZsNormalMasterWorkBService zsNormalMasterWorkBService;
    @Autowired
    protected OrderPayService orderPayService;

    /**
     * 新增订单验收
     *
     * @param addAcceptanceDIO
     * @return
     */
    @Override
    public ResponseDTO<AddOrderAcceptanceDRO> addOrderWorkAcceptance(AddAcceptanceDIO addAcceptanceDIO) {
        log.info("新增订单验收入参：{}", JSON.toJSONString(addAcceptanceDIO));
        try {
            // 服务完成验收需要检验，定金不需要
            if (Objects.equals(addAcceptanceDIO.getAcceptanceType(), OrderConsts.ORDER_ACCEPTANCE_TYPE_PAYMENT)) {
                CompleteDTO completeDTO = new CompleteDTO();
                completeDTO.setWorkId(addAcceptanceDIO.getWorkId());
                completeDTO.setOrderId(addAcceptanceDIO.getOrderId());
                zsNormalMasterWorkBService.checkComplete(completeDTO);
            }

            OrderWorkAcceptanceDTO dto = BeanMapper.map(addAcceptanceDIO, OrderWorkAcceptanceDTO.class);
            dto.setOperator(addAcceptanceDIO.getMasterName());
            dto.setOperatorId(Long.valueOf(addAcceptanceDIO.getMasterId()));
            dto.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_MASTER);
            dto.setOperateTime(DateUtil.getNow());
            ResultOrderAcceptanceDTO resultDTO = orderWorkAcceptanceBService.addOrderWorkAcceptance(dto);
            AddOrderAcceptanceDRO dro = BeanMapper.map(resultDTO, AddOrderAcceptanceDRO.class);
            return ResponseDTO.success(dro);
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("新增订单验收异常：[{}]", e.getMessage());
            e.printStackTrace();
            return ResponseDTO.fail();
        }
    }

    /**
     * 重新发送微信模板消息
     *
     * @param orderId
     * @param acceptanceId
     * @return
     */
    @Override
    public ResponseDTO sendOrderAcceptanceWeChatMessage(@NotNull(message = "入参不能为空") Long orderId, @NotNull(message = "入参不能为空") Long acceptanceId) {
        try {
            log.info("重新发送微信模板消息入参：{}-{}", orderId, acceptanceId);
            orderWorkAcceptanceBService.sendWeChatMessage(orderId, acceptanceId);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            log.error("重新发送验收微信模板消息异常：[{}]", e.getMessage());
            e.printStackTrace();
            return ResponseDTO.fail();
        }
    }
}
